package com.shigen.demo.base;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.MutableClassToInstanceMap;
import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.google.common.collect.Table;
import com.google.common.collect.TreeRangeMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import lombok.SneakyThrows;

/**
 * guava的相关demo
 *
 * @author shigenfu
 * @date 2022/12/2 20:04
 */
public class GuavaDemo {

    public static void main(String[] args) {
        // testString();
        // testList();
        // testMap();
        // testCollection();
        testCache();
    }

    @SneakyThrows
    private static void testCache() {
        // 创建一个缓存实例
        Cache<String, String> cache = CacheBuilder.newBuilder()
            // 最大容量为100个缓存项
            .maximumSize(100)
            // 10分钟后过期
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build();

        // 向缓存中存入数据
        cache.put("key1", "value1");
        cache.put("key2", "value2");

        // 从缓存中获取数据
        String value1 = cache.getIfPresent("key1");
        // 输出: Value1: value1
        System.out.println("Value1: " + value1);

        // 存中存在，则返回对应的值；如果缓存中不存在，则使用提供的函数加载新的值，并将其添加到缓存中
        String value3 = cache.get("key3", () -> "value3");
        // 输出: Value3: value3
        System.out.println("Value3: " + value3);

        // 获取缓存的统计信息
        CacheStats stats = cache.stats();
        // 输出: Hit Rate: 1.0
        System.out.println("Hit Rate: " + stats.hitRate());
    }


    private static void testCollection() {

        List<String> list = Lists.newArrayList("a", "b", "c");
        ImmutableList<String> immutableList = ImmutableList.copyOf(list);

        Iterable<String> iterable = Lists.newArrayList("a", "b", "c");
        Iterable<String> filtered = Iterables.filter(iterable, new Predicate<String>() {
            @Override
            public boolean apply(String s) {
                return s.startsWith("a");
            }
        });

        ArrayList<String> transformedIter = Lists.newArrayList(Iterables.transform(iterable, new Function<String, String>() {
            @Override
            public String apply(String s) {
                return Strings.repeat(s, 3);
            }
        }));
        System.out.println(transformedIter);
    }

    private static void testMap() {
        Table<String, String, Integer> hashBasedTable = HashBasedTable.create();
        HashBiMap<String, String> hashBiMap = HashBiMap.create();
        BiMap<String, String> inverse = hashBiMap.inverse();

        Multimap<String, String> multiMap = ArrayListMultimap.create();
        multiMap.putAll("name", Arrays.asList("shigen", "aaa", "bbb"));
        Collection<String> names = multiMap.get("name");
        Map<String, Collection<String>> stringCollectionMap = multiMap.asMap();

        RangeMap<Integer, String> rangeMap = TreeRangeMap.create();
        rangeMap.put(Range.closedOpen(0, 60), "fail");
        rangeMap.put(Range.closed(60, 90), "satisfactory");
        rangeMap.put(Range.openClosed(90, 100), "excellent");

        System.out.println(rangeMap.get(59));
        System.out.println(rangeMap.get(60));
        System.out.println(rangeMap.get(90));
        System.out.println(rangeMap.get(91));

        MutableClassToInstanceMap<List> listMutableClassToInstanceMap = MutableClassToInstanceMap.create();
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1111");
        listMutableClassToInstanceMap.putInstance(ArrayList.class, strings);
        ArrayList instance = listMutableClassToInstanceMap.getInstance(ArrayList.class);
        System.out.println(strings == instance);
    }


    private static void testList() {
        ArrayList<Integer> integers = Lists.newArrayList(12, 543, 534, 543, 4, 321432, 3, 56, 11);
        List<List<Integer>> partition = Lists.partition(integers, 2);
        System.out.println(partition);
    }

    private static void testString() {
        String test = "12,43,,,345432,43,4,,432";
        List<String> strings = Splitter.on(",").splitToList(test);
        System.out.println(strings);

        String s = Strings.emptyToNull("");
    }

}
